/**
 * fulihui.com Inc.
 * Copyright (c) 2015-2017 All Rights Reserved.
 */
package com.fulihui.information.service;

import com.alibaba.fastjson.JSON;
import com.fulihui.information.api.AwardService;
import com.fulihui.information.dal.dataobj.InfoUserBehaviorReward;
import com.fulihui.information.dal.dataobj.InfoUserBehaviorRewardExample;
import com.fulihui.information.dal.mapper.InfoUserBehaviorRewardMapper;
import com.fulihui.information.enums.AwardType;
import com.fulihui.information.enums.RewardedType;
import com.fulihui.information.model.UserRegisterAwardParam;
import com.fulihui.information.request.NewUserReadAwardRequest;
import com.fulihui.information.result.NewUserReadAwardResult;
import org.apache.dubbo.config.annotation.Service;
import org.near.servicesupport.result.ResultBuilder;
import org.near.toolkit.common.StringUtil;
import org.near.toolkit.security.codec.DESCoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 奖励服务
 *
 * @author: Levon
 * @version: v 0.1 2017-06-12 11:26
 */
@Service(version = "1.0.0")
public class AwardServiceImpl implements AwardService {

    private static final Logger logger = LoggerFactory.getLogger(AwardServiceImpl.class);

    private final static String DES_KEY = "SelJaqsOwTifr4krRaxibBf7";

    @Autowired
    InfoUserBehaviorRewardMapper infoUserBehaviorRewardMapper;



    /**
     * 新用户阅读奖励
     *
     * @param request
     * @return
     */
    @Override
    public NewUserReadAwardResult userRegisterAward(NewUserReadAwardRequest request) {


        logger.info("user register award is bengin!request={}", request);

        // 开始解密参数
        String param = request.getParam();
        if (StringUtil.isBlank(param)) {
            return ResultBuilder.fail(NewUserReadAwardResult.class, 1001, "param must not be null!");
        }

        UserRegisterAwardParam awardParam = null;
        try {
            //参数解密
            String decryptParam = DESCoder.decrypt(param, DES_KEY);
            awardParam = JSON.parseObject(decryptParam, UserRegisterAwardParam.class);
        } catch (Exception e) {
            logger.error("param decrypt is error,param={},error={}", param, e.getMessage());
            return ResultBuilder.fail(NewUserReadAwardResult.class, 1002, "parameter decryption failure!");
        }

        if (awardParam == null) {
            return ResultBuilder.fail(NewUserReadAwardResult.class, 1003, "parse json param is error,param is null!");
        }

        // 验证用户阅读的资讯数
        List<String> infoids = awardParam.getInfoids();
        if (CollectionUtils.isEmpty(infoids) || infoids.size() < 10) {
            return ResultBuilder.fail(NewUserReadAwardResult.class, 1004, "infoids is empty or length < 10!");
        }

        // 拿到userid
        String userid = awardParam.getUserid();
        if (StringUtil.isBlank(userid)) {
            return ResultBuilder.fail(NewUserReadAwardResult.class, 1005, "userId must not be null!");
        }

        //查询是否满足赠送条件
        InfoUserBehaviorRewardExample example = new InfoUserBehaviorRewardExample();
        InfoUserBehaviorRewardExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userid);
        criteria.andRTypeEqualTo(AwardType.REGISTER_CASH.getCode());
        List<InfoUserBehaviorReward> infoUserBehaviorRewards = infoUserBehaviorRewardMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(infoUserBehaviorRewards)) {
            return ResultBuilder.fail(NewUserReadAwardResult.class, 1006, "Is not a new user, ignore!");
        }

        //验证是否给这个用户送过钱了
        InfoUserBehaviorReward infoUserBehaviorReward = infoUserBehaviorRewards.get(0);
        Long amount = infoUserBehaviorReward.getAmount();
        Integer isRewarded = infoUserBehaviorReward.getIsRewarded();
        if (amount > 0L && RewardedType.REWARDED_TRUE.getFlag() == isRewarded) {
            return ResultBuilder.fail(NewUserReadAwardResult.class, 1007, "The user has already handed out awards!");
        }

        return new NewUserReadAwardResult();

    }


}
